Kotlin与Java的异同看完这篇就够了!

您所在的位置:网站首页 安卓 java kotlin Kotlin与Java的异同看完这篇就够了!

Kotlin与Java的异同看完这篇就够了!

2023-03-15 05:00| 来源: 网络整理| 查看: 265

在这篇文章中呢,我将为大家分享Kotlin与Java上的主要异同,希望能够帮助到各位小伙伴利用现有的Java知识快速上手Kotlin开发。

本文出自教程《移动端架构师成长体系课》——带你完成从普通工程师到架构师的蜕变。打印日志JavaSystem.out.print("hello world"); System.out.println("hello world");Kotlinprint("hello world") println("hello world")定义变量与常量常JavaString name = "hello world"; final String name = "hello world";Kotlinvar name = "hello world" val name = "hello world"null声明JavaString otherName; otherName = null;Kotlinvar otherName : String? otherName = null空判断Javaif (text != null) { int length = text.length(); }

Kotlintext?.let { val length = text.length } // or simply val length = text?.length

字符串拼接JavaString firstName = "Android"; String lastName = "Architect"; String message = "My name is: " + firstName + " " + lastName;Kotlinval firstName = "Android" val lastName = "Architect" val message = "My name is: $firstName $lastName"换行JavaString text = "First Line\n" + "Second Line\n" + "Third Line";

Kotlinval text = """ |First Line |Second Line |Third Line """.trimMargin()

三元表达式JavaString text = x > 5 ? "x > 5" : "x 5" else "x


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3